package com.qiangesoft.blog.common.utils;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;

/**
 * 代码生成工具类
 *
 * @author qiange
 * @date 2023-06-01
 */
public class GeneratorUtils {

    public static void main(String[] args) {
        autoGenerator();
    }

    public static void autoGenerator() {
        AutoGenerator autoGenerator = new AutoGenerator();
        autoGenerator.setDataSource(getDataSourceConfig());
        autoGenerator.setGlobalConfig(getGlobalConfig());
        autoGenerator.setPackageInfo(getPackageInfo());
        autoGenerator.setStrategy(getStrategyConfig());
        autoGenerator.execute();
    }

    /**
     * 设置数据源
     *
     * @return
     */
    public static DataSourceConfig getDataSourceConfig() {
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://127.0.0.1:3306/blog?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=GMT%2B8");
        dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("root");
        dsc.setDbType(DbType.MYSQL);
        return dsc;
    }

    /**
     * 设置全局配置
     *
     * @return
     */
    public static GlobalConfig getGlobalConfig() {
        GlobalConfig gc = new GlobalConfig();
        String path = System.getProperty("user.dir");
        gc.setOutputDir(path + "/qiange-blog-core/src/main/java");
        gc.setAuthor("qiange");
//        gc.setOpen(true);
        gc.setFileOverride(true);
        // id生成策略
        gc.setIdType(IdType.INPUT);
        gc.setDateType(DateType.TIME_PACK);
        return gc;
    }

    /**
     * 包配置
     *
     * @return
     */
    public static PackageConfig getPackageInfo() {
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.qiangesoft.blog.core");
        pc.setEntity("entity");
        pc.setMapper("mapper");
        pc.setService("service");
        pc.setServiceImpl("service.impl");
        pc.setController("controller");
        return pc;
    }

    /**
     * 策略配置
     *
     * @return
     */
    public static StrategyConfig getStrategyConfig() {
        StrategyConfig strategy = new StrategyConfig();
        // 下划线转驼峰命名
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);

        // todo 设置映射的表名，多张表
        strategy.setInclude("blog_category");
//        strategy.setTablePrefix("sys_");
        // 是否启用lombok开启注解
        strategy.setEntityLombokModel(true);
        // 设置逻辑删除字段
        strategy.setLogicDeleteFieldName("deleted");

        // 时间自动填充配置
        TableFill startDate = new TableFill("create_time", FieldFill.INSERT);
        TableFill updateDate = new TableFill("update_time", FieldFill.INSERT_UPDATE);

        ArrayList<TableFill> list = new ArrayList<>();
        list.add(startDate);
        list.add(updateDate);
        strategy.setTableFillList(list);

        // 乐观锁配置
//        strategy.setVersionFieldName("version");
        // rustful 格式
        strategy.setRestControllerStyle(true);

        return strategy;
    }

}